package org.example.easybuy.service.impl;

import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.example.easybuy.entity.Product;
import org.example.easybuy.service.CategoryService;
import org.example.easybuy.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private CategoryService cateService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public PageInfo<Product> getEsPageInfo(Map<String, String> map) {
        int pageNum=Integer.parseInt(map.get("pageNum"));
        int pageSize=Integer.parseInt(map.get("pageSize"));
        NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder();
        if(!StringUtils.isNullOrEmpty(map.get("name")))
        {
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("name",map.get("name")));
            HighlightBuilder highlightBuilder=new HighlightBuilder();
            highlightBuilder.field("name");
            highlightBuilder.preTags("<span style='color:red;'>");
            highlightBuilder.postTags("</span>");
            nativeSearchQueryBuilder.withHighlightBuilder(highlightBuilder);
        }
        if(map.containsKey("id"))
        {
            int id=Integer.parseInt(map.get("id"));
            int type=cateService.selectTypeById(id);
            if(type==1)
            {

                nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("categoryLevel1Id",id));
            }
            else if(type==2)
            {
                nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("categoryLevel2Id",id));
            }
            else
            {
                nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("categoryLevel3Id",id));
            }
        }
        nativeSearchQueryBuilder
                .withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC))
                .withPageable(PageRequest.of((pageNum-1),pageSize));

        List<Product> list=new ArrayList<>();
        SearchHits<Product> hits= elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(),Product.class);
        Map<String,List<String>> highMap;
        for(SearchHit<Product> searchHit:hits.getSearchHits())
        {
            Product product=searchHit.getContent();
            highMap=searchHit.getHighlightFields();
            if(!highMap.isEmpty())
            {
                product.setName(highMap.get("name").get(0));
            }
            list.add(product);
        }
        PageInfo<Product> pageInfo=new PageInfo<>();
        pageInfo.setList(list);
        pageInfo.setTotal(hits.getTotalHits());
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        int pages=(int) (pageInfo.getTotal()%pageInfo.getPageSize()==0?pageInfo.getTotal()/pageInfo.getPageSize():pageInfo.getTotal()/pageInfo.getPageSize()+1);
        pageInfo.setPages(pages);
        return pageInfo;
    }
}
